package com.cn.wanxi.util;

import com.cn.wanxi.model.NavModel;
import com.sun.deploy.util.StringUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.cn.wanxi.util.MyPath.SPLIT;

public class WriteRead {
    public static void writeNote(String fileName, List<String> list) {
        File fileDirectory = new File(MyEnum.PATH.getPath());
        if (!fileDirectory.exists()) {
            fileDirectory.mkdirs();
        }
        File file = new File(fileDirectory, fileName);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            OutputStream outputStream = new FileOutputStream(file);
            for (int i = 0; i < list.size(); i++) {
                outputStream.write(list.get(i).getBytes());
                outputStream.write(SPLIT.getBytes());
            }

            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String readNote(String fileName) {
        File file = new File(MyEnum.PATH.getPath(), fileName);
        StringBuilder string = new StringBuilder();
        /**
         * StringBuilder 和 String 和StringBuffer 的区别？
         * 抽象类和接口？
         * final  finally  finalize 的区别？
         *
         */
        try {
//            InputStream inputStream = new FileInputStream(file);
//            int result;

//            for (int i=0;i<file.length();i++){
//
//            }
//            while ((result = inputStream.read()) != -1) {
//                System.out.print((char) result);
//            }
//            字节和字符
//            System.out.println((char) result);
            Reader reader = new FileReader(file);
            int result;

            while ((result = reader.read()) != -1) {
                string.append((char) result);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return string.toString();
    }
}
